package com.example.templateapp.ui.viewFlipper

import android.os.Bundle
import android.view.LayoutInflater
import android.view.animation.Animation
import android.view.animation.LinearInterpolator
import android.view.animation.TranslateAnimation
import android.widget.SeekBar
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import com.example.templateapp.R
import com.example.templateapp.databinding.ActivityViewFlipperBinding
import com.example.templateapp.util.LifecycleUtil

class ViewFlipperActivity : AppCompatActivity() {

    private lateinit var binding: ActivityViewFlipperBinding
    private lateinit var lifecycle: LifecycleUtil


    private var mDuration: Long = 50


    private val inTranslateAnimation = TranslateAnimation(
        Animation.RELATIVE_TO_SELF, -1f,
        Animation.RELATIVE_TO_SELF, 0f,
        Animation.RELATIVE_TO_SELF, 0f,
        Animation.RELATIVE_TO_SELF, 0f
    )
    private val outTranslateAnimation = TranslateAnimation(
        Animation.RELATIVE_TO_SELF, 0f,
        Animation.RELATIVE_TO_SELF, 1f,
        Animation.RELATIVE_TO_SELF, 0f,
        Animation.RELATIVE_TO_SELF, 0f
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_view_flipper)
        with(binding.viewFlipper) {
            clearFocus()
            for (i in 1..2) {
                val view = LayoutInflater.from(this@ViewFlipperActivity)
                    .inflate(R.layout.viewfilpper_item, null)
                addView(view)
            }

            val linearInterpolator = LinearInterpolator()
            inTranslateAnimation.interpolator = linearInterpolator
            outTranslateAnimation.interpolator = linearInterpolator

            inAnimation = inTranslateAnimation
            outAnimation = outTranslateAnimation

            refreshFilp()
        }

        binding.seekBar.max = 10
        binding.seekBar.setOnSeekBarChangeListener(listener)
        binding.seekBar.tag = 100
        binding.seekBar2.max = 1000
        binding.seekBar2.setOnSeekBarChangeListener(listener)
        binding.seekBar2.tag = 1


    }

    private val listener = object : SeekBar.OnSeekBarChangeListener {
        override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
            val times = seekBar?.tag as Int
            mDuration = (progress * times).toLong()
            binding.textView2.text = "${mDuration}ms"
        }

        override fun onStartTrackingTouch(seekBar: SeekBar?) {}

        override fun onStopTrackingTouch(seekBar: SeekBar?) {
            refreshFilp()
        }
    }

    private fun refreshFilp() {
        inTranslateAnimation.duration = mDuration
        outTranslateAnimation.duration = mDuration
        binding.viewFlipper.flipInterval = mDuration.toInt()
        binding.viewFlipper.startFlipping()
    }

}